package m.jclecai.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.List;

/**
 * Created by hp on 2017/3/23.
 */

public class PopWindowAdapter extends BaseAdapter {
    private Context ctxt;
    private String[] arr;
    private String[] types;
    private List<String> id;

    public PopWindowAdapter(Context ctxt, String[] arr, String[] types) {
        this.ctxt = ctxt;
        this.arr = arr;
        this.types = types;
    }

    public PopWindowAdapter(Context ctxt, String[] arr, String[] types,
                            List<String> type) {
        this.ctxt = ctxt;
        this.arr = arr;
        this.types = types;
        this.id = type;
    }

    @Override
    public int getCount() {
        return arr.length;
    }

    @Override
    public Object getItem(int position) {
        return arr[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(ctxt).inflate(
                    R.layout.fragment_hemai_popuwindow_item, null);

            viewHolder.txt_hemai_popuwindow = (TextView) convertView
                    .findViewById(R.id.txt_hemai_popuwindow);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String name = arr[position];
        String type = types[position];
        viewHolder.txt_hemai_popuwindow.setText(name);
        if (id != null&&id.size()>0) {
            //id.get(0)就是当前选择的这个条目
            String t = id.get(0);
            if (type.equals(t)) {
                viewHolder.txt_hemai_popuwindow
                        .setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg_press);
            } else {
                viewHolder.txt_hemai_popuwindow
                        .setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
            }
        }
        //给每一个条目上的TextView添加标记
        viewHolder.txt_hemai_popuwindow.setTag(type);
        return convertView;
    }

    static class ViewHolder {
        public TextView txt_hemai_popuwindow;
    }
}
